<template>
    <div>
    <el-upload v-if="type == 'image'" class="elUpload" :accept="'image/png,image/gif,image/jpeg,image/jpg,image/bmp'" :action="action" :multiple="false" :data="data" :show-file-list="false" :on-success="uploadSuccessFun"></el-upload>
    <el-upload v-else-if="type == 'video'" accept="video/*" :action="action" class="elUpload"  :multiple="false" :data="data" :show-file-list="false" :on-success="uploadSuccessFun"></el-upload>
    <el-upload v-else class="elUpload"  :multiple="false" :action="action" :data="data" :show-file-list="false" :on-success="uploadSuccessFun"></el-upload>
    </div>
</template>

<script>
import CONFIG from "@/utils/config"
import _cache from "@/utils/cache"
export default {
    data(){
        return {
            action:CONFIG.baseURL + "api/upload",
            data:{
                token:_cache.get("token") || '',
            },
            uploadOpt:{
                accept:''
            },
            type:'file',
            event:''
        }
    },
    methods:{
        options(opt={}){
            this.type = opt.type || 'file'
            this.event = opt.event || ''
            this.$nextTick(()=>{
                document.querySelector('.elUpload .el-upload__input').click()
            })
        },
        uploadSuccessFun(res){
            if(res.code == 200){
                this.$emit('success',res.data,this.type,this.event)
            }else{
                this.$message.error(res.msg);
            }
        },
    }
}
</script>


<style lang="less" scoped>
.elUpload {
    position: fixed;
    left: -1000px;
    top: -1000px;
}
</style>